package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class setmealController {
    @Reference
    private SetMealService setMealServicel;
    @Autowired
    private JedisPool jedisPool;
///setmeal/upload.do  imgFile:

    /**
     *   上传图片
     * @return
     */
    @RequestMapping("/upload.do")
    public Result upload(@RequestParam(value = "imgFile") MultipartFile imgFile){
//       1.获得文件名称
//        System.out.println(imgFile);
        String originalFilename = imgFile.getOriginalFilename();
        int index = originalFilename.lastIndexOf(".");
//        得到后缀类型
        String suffix = originalFilename.substring(index);
//        使用UUID取前缀名字--取名字
       String fileName= UUID.randomUUID().toString()+suffix;
        try {
            //         使用工具类上传图片
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName );
//           图片服务器中的redis
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
        } catch (Exception e) {
            return  new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    /**
     *  添加套餐基本信息和中间表
     * setmeal/add.do?checkgroupIds="+this.checkgroupIds,this.formData
     */
    @RequestMapping("/add.do")
    public Result  add(Integer[]checkgroupIds, @RequestBody Setmeal setmeal){
        try{
            setMealServicel.add(checkgroupIds,setmeal);
            return  new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        }catch (Exception e){
            return  new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    /**
     *   分页查询
     * "/setmeal/findAllSetMeal",parm)
     */
    @RequestMapping("/findAllSetMeal")
    public Result findAllSetMeal(@RequestBody QueryPageBean queryPageBean){
        try{
            PageResult pageResult= setMealServicel.findAllSetMeal(queryPageBean);
            return  new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS, pageResult);
        }catch (Exception e){
            return  new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }
    /**
     * 根据id--回显套餐基本信息
     *  /setmeal/findSetMealById.do?id="+row.id
     *  /setmeal/findSetMealById.do?id
     */
   @RequestMapping("/findSetMealById")
    public Result  findSetMealById(Integer id){
        try{
            Setmeal setmeal= setMealServicel.findSetMealById(id);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    /**
     * //  根据套餐id查看--勾选项目
     * /setmeal/findGroups.do?id="+row.id
     */
    @RequestMapping("/findGroups")
    public Result findGroups(Integer id){
        try{
            Integer[]groups=setMealServicel.findGroups(id);
            return  new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,groups);
        }catch (Exception e){
            return  new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
    /**
     *  更改套餐
     * /setmeal/edit.do?checkgroupIds="+this.checkgroupIds,this.formData
     */
    @RequestMapping("/edit")
    public Result edit(Integer[]checkgroupIds,@RequestBody Setmeal setmeal){
        try{
            setMealServicel.edit(checkgroupIds,setmeal);
            return  new Result(true,MessageConstant.GET_EDIT_SETITEM_SUCCESS);
        }catch (Exception e){
            return  new Result(false,MessageConstant.GET_EDIT_SETITEM_FAIL);
        }
    }
}

